package com.ruoyi.web.controller.materials;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.materials.service.impl.LwMaterialServiceImpl;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.materials.domain.LwMaterial;
import com.ruoyi.materials.service.ILwMaterialService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 原材料Controller
 * 
 * @author sl
 * @date 2023-08-04
 */
@RestController
@RequestMapping("/materials/material")
public class LwMaterialController extends BaseController
{
    @Autowired
    private ILwMaterialService lwMaterialService;

    /**
     * 查询原材料列表
     */
    @PreAuthorize("@ss.hasPermi('materials:material:list')")
    @GetMapping("/list")
    public TableDataInfo list(LwMaterial lwMaterial)
    {
        startPage();
        List<LwMaterial> list = lwMaterialService.selectLwMaterialList(lwMaterial);
        return getDataTable(list);
    }

    /**
     * 查询后焊器件列表
     */
    @GetMapping("/backWeldMaterialList")
    public TableDataInfo backWeldMaterialList(LwMaterial lwMaterial)
    {
        startPage();
        List<LwMaterial> list = lwMaterialService.selectBackWeldMaterialList(lwMaterial);
        return getDataTable(list);
    }


    /**
     * 导出原材料列表
     */
    @PreAuthorize("@ss.hasPermi('materials:material:export')")
    @Log(title = "原材料", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LwMaterial lwMaterial)
    {
        List<LwMaterial> list = lwMaterialService.selectLwMaterialList(lwMaterial);
        ExcelUtil<LwMaterial> util = new ExcelUtil<LwMaterial>(LwMaterial.class);
        util.exportExcel(response, list, "原材料数据");
    }

    /**
     * 获取原材料详细信息
     */
    @PreAuthorize("@ss.hasPermi('materials:material:query')")
    @GetMapping(value = "/{materialId}")
    public AjaxResult getInfo(@PathVariable("materialId") Long materialId)
    {
        return success(lwMaterialService.selectLwMaterialByMaterialId(materialId));
    }

    /**
     * 新增原材料
     */
    @PreAuthorize("@ss.hasPermi('materials:material:add')")
    @Log(title = "原材料", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LwMaterial lwMaterial)
    {
        return toAjax(lwMaterialService.insertLwMaterial(lwMaterial));
    }

    /**
     * 新增后焊材料
     */
    @PutMapping("/addBackweldMaterial")
    public AjaxResult addBackweldMaterial(@RequestBody LwMaterial lwMaterial)
    {
        return toAjax(lwMaterialService.addBackweldMaterial(lwMaterial));
    }

    /**
     * 修改原材料
     */
    @PreAuthorize("@ss.hasPermi('materials:material:edit')")
    @Log(title = "原材料", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LwMaterial lwMaterial)
    {
        return toAjax(lwMaterialService.updateLwMaterial(lwMaterial));
    }

    /**
     * 原材料出入库
     */
    @PutMapping("/updateMaterialQuantity")
    public AjaxResult updateMaterialQuantity(@RequestBody LwMaterial lwMaterial)
    {
        int result = lwMaterialService.updateMaterialQuantity(lwMaterial);
        if (result == -1) {
            return AjaxResult.error("参数有误");
        } else if (result == -2) {
            return AjaxResult.error("库存不足");
        } else {
            return toAjax(result);
        }
    }

    /**
     * 删除后焊材料
     */
    @PutMapping("/delBackweldMaterial/{materialIds}")
    public AjaxResult delBackweldMaterial(@PathVariable Long materialIds)
    {
        return toAjax(lwMaterialService.delBackweldMaterial(materialIds));
    }
    /**
     * 删除原材料
     */
    @PreAuthorize("@ss.hasPermi('materials:material:remove')")
    @Log(title = "原材料", businessType = BusinessType.DELETE)
	@DeleteMapping("/{materialIds}")
    public AjaxResult remove(@PathVariable Long[] materialIds)
    {
        return toAjax(lwMaterialService.deleteLwMaterialByMaterialIds(materialIds));
    }

    /**
     * 导入原材料入库数据
     */
    @Log(title = "原材料管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean isUpdateSupport) throws Exception
    {
        ExcelUtil<LwMaterial> util = new ExcelUtil<>(LwMaterial.class);
        List<LwMaterial> lwMaterialList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = lwMaterialService.importUser(lwMaterialList, isUpdateSupport, operName);
        return AjaxResult.success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<LwMaterial> util = new ExcelUtil<>(LwMaterial.class);
        util.importTemplateExcel(response, "项目编号数据");
    }
}
